﻿var vm = new Vue({
    el: "#PAGE_MODEL",
    data: {
        //删除时传入的ID参数
        qid: "",

        //查询关联明细表的关键字
        keyword: '',

        //查询企业资质表时的关键字-项目类型/名称关键字
        one_keyword: "",

        //查询企业资质表时的关键字-项目内容关键字
        two_keyword: "",

        ////查询企业资质表时的关键字-资质要求关键字
        three_keyword: "",

        //多条信息
        VmQualifications: [],

        //单个类
        VmQualification:
        {
            //ID
            Qid: "",

            //项目类型
            Type: "",

            //项目内容
            Content: "",

            //备注
            Remark: "",

            //企业资质明细列表
            VmQualificationDetails: [],

            //附件列表
            VmAttachments: []
        },

        //备选_企业资质明细列表
        VmQualificationDetails_BX: [],

        //索引
        pageindex: 0,

        //每页记录数
        pagesize: 10,

        //总页数
        pagecount: 1,

        //分页标志
        pagesizes: [10, 20, 30, 40, 50],

        //当前页
        currentpage: 1,

        //总页数
        total: 0,

    },
    methods: {
        //删除附件
        DeleteAttachment: function (id) {
            var self = this;

            if (self.VmQualification !== null && self.VmQualification.Qid !== "") {
                $.ajax({
                    url: URLs.DELETE_ATTACHMENT,
                    type: "POST",
                    dataType: "json",
                    data: JSON.stringify({
                        id: id
                    }),
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        if (data.ISOK) {
                            //self.GetAttachmentFiles();
                            bootbox.alert("删除附件成功！");
                            self.getDataFromListToUpdate(self.VmQualification.Qid);
                        } else {
                            bootbox.alert("操作错误（删除文件），错误代码：<br />" + data.MESSAGE);
                            $.jqhideloading();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        bootbox.alert("系统运行错误（删除文件），错误代码：<br />" + textStatus);
                        $.jqhideloading();
                    }
                });
            }
        },

        //上传附件
        uploadAttachment: function () {
            var self = this;

            var options = {
                target: null,
                beforeSubmit:
                    function () {
                        $.jqshowloading();
                    },
                success:
                    function (xhr) {
                        $.jqhideloading();
                        if (xhr.ISOK) {

                            //上传成功，刷新页面
                            window.location.reload();

                        } else {
                            bootbox.alert("系统运行错误，错误代码：<br />" + xhr.MESSAGE);
                        }
                    },
                complete:
                    function (xhr) {
                        $.jqhideloading();
                    },
                url: URLs.URL_UPLOAD_FILES,
                type: "post"
            };
            $('#UpAttachment').ajaxForm(options).submit();
        },

        //跳转到新建页面
        NewQualification: function () {
            location.href = URLs.URL_GOTO_NEWQUALIFICATION;
        },

        //添加企业资质记录
        addQualification: function () {
            var self = this;

            if (self.VmQualification.Type.length > 0 && self.VmQualification.Content.length > 0) {
                //拼凑参数
                var data = self.VmQualification;

                $.ajax({
                    url: URLs.URL_ADD_QUALIFICATION,
                    type: "POST",
                    dataType: "json",
                    data: JSON.stringify({
                        VmQualification: data
                    }),
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        if (data.ISOK) {

                            bootbox.alert("保存记录成功!");

                        } else {
                            bootbox.alert("保存错误，错误代码：<br />" + data.MESSAGE);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        bootbox.alert("保存记录基本信息时，系统运行错误，错误代码：<br />" + textStatus);
                        $.jqhideloading();
                    },
                    beforeSend: function () {
                        $.jqshowloading();
                    },
                    complete: function () {
                        $.jqhideloading();
                    }
                });

            } else {
                bootbox.alert("您还没填写完整信息，请继续填写");
            }
        },

        //删除企业资质记录
        DeleteQualification: function (qid) {
            var self = this;

            bootbox.confirm("确定要删除选定的记录吗？", function (result) {
                if (result) {
                    //删除记录
                    $.ajax({
                        type: "POST",
                        url: URLs.URL_DELETE_QUALIFICATION,
                        dataType: "json",
                        data: JSON.stringify({
                            qid: qid
                        }),
                        contentType: 'application/json; charset=utf-8',
                        success: function (data) {
                            if (data.ISOK) {
                                bootbox.alert("成功删除指定数据！");
                                self.getQualificationListindex();
                            } else {
                                bootbox.alert("删除数据错误：" + data.MESSAGE);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            bootbox.alert("系统错误！");
                            //初始化错误结束，允许按钮，并将图标恢复原状
                        },
                        beforeSend: function () {
                            $.jqshowloading();
                        },
                        complete: function () {
                            $.jqhideloading();
                        }

                    });
                } else {
                    return 0;
                }
            });
        },

        //当前页多少条记录
        handleCurrentChange: function (index) {
            var self = this;
            //选中的第几页
            self.getQualificationListCurrent(index);
            console.log(index);
        },

        //查询企业资质记录列表---handleCurrentChange
        getQualificationListCurrent: function (index) {
            var self = this;
            self.pageindex = index - 1;

            $.ajax({
                url: URLs.URL_GET_QUALIFICATIONLIST,
                type: "POST",
                dataType: "json",
                data: JSON.stringify({
                    one_keyword: $("#one_keyword").val(),
                    two_keyword: $("#two_keyword").val(),
                    three_keyword: $("#three_keyword").val(),
                    pageindex: self.pageindex,
                    pagesize: self.pagesize,
                }),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.ISOK) {

                        self.VmQualifications = data.VmQualifications;

                        //绑定查询条件
                        self.pageindex = data.PageIndex;
                        self.pagecount = data.PageCount;
                        self.total = data.RecordCount;

                    } else {
                        bootbox.alert("查询信息错误，错误代码：<br />" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("系统运行错误（获取记录列表），错误代码：<br />" + textStatus);
                    $.jqhideloading();
                },
                beforeSend: function () {
                    $.jqshowloading();
                },
                complete: function () {
                    $.jqhideloading();
                }
            });
        },

        //每页多少条记录
        handleSizeChange: function (size) {
            var self = this;
            self.currentpage = 1;
            self.pagesize = size;
            self.pageindex = 0;
            self.getQualificationListindex();
            console.log(size);
        },

        //查询企业资质记录列表--handleSizeChange
        getQualificationListindex: function () {
            var self = this;
            $.ajax({
                url: URLs.URL_GET_QUALIFICATIONLIST,
                type: "POST",
                dataType: "json",
                data: JSON.stringify({
                    one_keyword: $("#one_keyword").val(),
                    two_keyword: $("#two_keyword").val(),
                    three_keyword: $("#three_keyword").val(),
                    pageindex: self.pageindex,
                    pagesize: self.pagesize,
                }),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.ISOK) {

                        self.VmQualifications = data.VmQualifications;

                        //绑定查询条件
                        self.pageindex = data.PageIndex;
                        self.pagecount = data.PageCount;
                        self.total = data.RecordCount;

                    } else {
                        bootbox.alert("查询信息错误，错误代码：<br />" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("系统运行错误（获取记录列表），错误代码：<br />" + textStatus);
                    $.jqhideloading();
                },
                beforeSend: function () {
                    $.jqshowloading();
                },
                complete: function () {
                    $.jqhideloading();
                }
            });
        },

        //修改时候捕捉数据显示在修改页面中
        getDataFromListToUpdate: function (qid) {
            var self = this;
            $.ajax({
                url: URLs.URL_GET_ONEQUALIFICATION,
                type: "POST",
                dataType: "json",
                data: JSON.stringify({
                    qid: qid
                }),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.ISOK) {

                        self.VmQualification = data.VmQualification;

                    } else {
                        bootbox.alert("获取修改信息错误，错误代码：<br />" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("系统运行错误（获取需要修改的这条记录），错误代码：<br />" + textStatus);
                    $.jqhideloading();
                },
                beforeSend: function () {
                    $.jqshowloading();
                },
                complete: function () {
                    $.jqhideloading();
                }
            });
        },

        //修改企业资质记录
        UpdateQualification: function () {
            var self = this;
            //拼凑参数
            var data = self.VmQualification;
            $.ajax({
                url: URLs.URL_UPDATE_QUALIFICATION,
                type: "POST",
                dataType: "json",
                data: JSON.stringify({
                    VmQualification: data
                }),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.ISOK) {
                        bootbox.alert("修改信息成功!");
                    } else {
                        bootbox.alert("修改信息错误，错误代码：<br />" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert("修改信息时，系统运行错误，错误代码：<br />" + textStatus);
                    $.jqhideloading();
                },
                beforeSend: function () {
                    $.jqshowloading();
                },
                complete: function () {
                    $.jqhideloading();
                }
            });
        },

        //查询上一页数据
        pagePrev: function () {
            var self = this;

            if (self.pageindex - 1 >= 0) {
                self.pageindex--;
                self.getQualificationListindex();
            } else {
                bootbox.alert("已经是第一页了！");
            }
        },

        //查询下一页数据
        pageNext: function () {
            var self = this;

            if (self.pageindex + 1 < self.pagecount) {
                self.pageindex++;
                self.getQualificationListindex();
            } else {
                bootbox.alert("已经是最后一页了！");
            }
        },

        //---------------------------------关联明细▼---------------------------------

        //打开明细选择窗
        showmodalForSelectQualificationDetails: function () {
            this.keyword = "";
            this.VmQualificationDetails = [];
            $("#FormSelectVmQualificationDetails").modal('show');
        },

        //获取明细列表信息
        searchQualificationDetails: function () {
            var self = this;
            if (self.keyword === "") {
                bootbox.alert("没有设置查询条件，会导致数据返回过多！");
            } else {
                $.ajax({
                    url: URLs.URL_GET_VMQUALIFICATIONDETAILS,
                    type: "POST",
                    dataType: "json",
                    data: JSON.stringify({
                        keyword: self.keyword
                    }),
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        if (data.ISOK) {
                            self.VmQualificationDetails_BX.splice(0, self.VmQualificationDetails.length);
                            self.VmQualificationDetails_BX = data.VmQualificationDetails;
                        } else {
                            bootbox.alert("获取明细信息记录错误，错误代码：" + data.MESSAGE);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        bootbox.alert("系统运行错误，错误代码：<br />" + textStatus);
                    }
                });
            }
        },

        //选择明细
        selectQualificationDetails: function (e) {
            var self = this;

            //循环判断是否已经包括相同ID的明细记录
            var _has_item = false;
            for (i = 0; i < self.VmQualification.VmQualificationDetails.length; i++) {
                if (self.VmQualification.VmQualificationDetails[i].DetailId == e.DetailId) {
                    _has_item = true;
                    break;
                }
            }

            if (!_has_item) {
                //不包括则添加，否则不予操作
                var A = {
                    DetailId: e.DetailId,
                    Details_Require: e.Details_Require,
                    Details_Range: e.Details_Range,
                    Details_Remark: e.Details_Remark,
                };
                self.VmQualification.VmQualificationDetails.push(A);
            }

        },

        //删除指定的明细
        deleteQualificationDetails: function (e) {
            var self = this;

            //循环VmQualificationDetails，获取要删除的关联明细的索引
            var _idx = -1;
            for (i = 0; i < self.VmQualification.VmQualificationDetails.length; i++) {
                if (self.VmQualification.VmQualificationDetails[i].DetailId == e.DetailId) {
                    _idx = i;
                }
            }

            //删除数组中的指定明细项
            if (_idx >= 0) {
                self.VmQualification.VmQualificationDetails.splice(_idx, 1);
            }

        },

        //---------------------------------关联明细▲---------------------------------
    }
});